package com.naodong.shenluntiku.mvp.view.adapter;

import android.text.TextUtils;

import com.blankj.utilcode.util.NetworkUtils;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.naodong.shenluntiku.R;
import com.naodong.shenluntiku.mvp.model.bean.OnceTipType;
import com.naodong.shenluntiku.mvp.model.bean.Study;
import com.naodong.shenluntiku.mvp.model.bean.StudyWrapper;
import com.naodong.shenluntiku.mvp.model.data.manager.BoxManager;
import com.naodong.shenluntiku.mvp.presenter.StudyPresenter;
import com.naodong.shenluntiku.mvp.view.activity.AnalysisDetailActivityAutoBundle;
import com.naodong.shenluntiku.mvp.view.activity.AssessmentActivity;
import com.naodong.shenluntiku.mvp.view.activity.AssessmentActivityAutoBundle;
import com.naodong.shenluntiku.mvp.view.widget.MaterialDialog;
import com.squareup.phrase.Phrase;


/**
 * Created by Administrator on 2017/5/30.
 */

public class StudyAdapter extends BaseMultiItemQuickAdapter<StudyWrapper, BaseViewHolder> {

    StudyPresenter presenter;

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     */
    public StudyAdapter(StudyPresenter presenter) {
        super(null);
        this.presenter = presenter;
        addItemType(StudyWrapper.TITLE, R.layout.study_item_title);
        addItemType(StudyWrapper.QUICK_START, R.layout.study_item_quick_start);
        addItemType(StudyWrapper.SUBJECT, R.layout.study_item_subject);
        addItemType(StudyWrapper.VIDEO, R.layout.study_item_video);
    }


    @Override
    protected void convert(BaseViewHolder holder, StudyWrapper item) {

        if (holder.getItemViewType() == StudyWrapper.TITLE) {

            StudyWrapper titleItem = item;
            holder.setText(R.id.titleTV, titleItem.getDprTitle());

            String completeText = Phrase.from("已有{num}人全部完成").put("num", titleItem.getDrTaskCompleteNum()).format().toString();
            holder.setText(R.id.complteTV, completeText);
            holder.setText(R.id.taskNumTV, titleItem.getDprTaskCompleteNum() + "/" + titleItem.getDprTaskNum());
            if (titleItem.getDprTaskNum() != 0) {
                holder.setProgress(R.id.progressBar, titleItem.getDprTaskCompleteNum() * 100 / titleItem.getDprTaskNum());
            } else {
                holder.setProgress(R.id.progressBar, 0);
            }
        } else if (holder.getItemViewType() == StudyWrapper.QUICK_START) {
            Study.Content content = item.getItem();
            holder.setText(R.id.subTitleTV, content.getTitle());
            holder.setText(R.id.desTV, content.getDesc());
            holder.setText(R.id.optionBtn, content.isFinish() ? "继续练习" : "练习");
            holder.setText(R.id.completeStudyTV, Phrase.from("已有{num}人完成学习").put("num", item.getDrAssessCompleteNum()).format());
            holder.itemView.setOnClickListener(v -> {
                if (!TextUtils.isEmpty(content.getTips())) {
                    if (!BoxManager.getInstance().queryBoxOnceStatusByType(OnceTipType.study_message_tip + content.getRcId())) {
                        BoxManager.getInstance().putBoxOnce(OnceTipType.study_message_tip + content.getRcId(), true);
                        mContext.startActivity(AssessmentActivityAutoBundle.builder(content.getTitle(), content.getRcId(), AssessmentActivity.FROM_STUDY).tip(content.getTips()).build(mContext));
                        return;
                    }
                }
                mContext.startActivity(AssessmentActivityAutoBundle.builder(content.getTitle(), content.getRcId(), AssessmentActivity.FROM_STUDY).build(mContext));
            });

        } else if (holder.getItemViewType() == StudyWrapper.SUBJECT) {
            Study.Content content = item.getItem();
            holder.setText(R.id.subTitleTV, content.getTitle());
            holder.setText(R.id.desTV, content.getDesc());
            switch (content.getRecommendType()) {
                case "rec_article_fenxi":
                    holder.setBackgroundRes(R.id.iconTV, R.drawable.ico_fenxi);
                    break;
                case "rec_article_duice":
                    holder.setBackgroundRes(R.id.iconTV, R.drawable.ico_duice);
                    break;
                case "rec_article_yingyongwen":
                    holder.setBackgroundRes(R.id.iconTV, R.drawable.ico_yingyong);
                    break;
                case "rec_article_zuowen":
                    holder.setBackgroundRes(R.id.iconTV, R.drawable.ico_zuowen);
                    break;
                case "rec_article_gaikuo":
                    holder.setBackgroundRes(R.id.iconTV, R.drawable.ico_gaikuo);
                    break;
            }
            holder.itemView.setOnClickListener(v -> {

                toAnalysisDetail(content);
            });

        } else if (holder.getItemViewType() == StudyWrapper.VIDEO) {
            Study.Content content = item.getItem();
            holder.setText(R.id.subTitleTV, content.getTitle());
            holder.itemView.setOnClickListener(v -> {

                onClickPlay(content);
            });
        }
    }

    private void toAnalysisDetail(Study.Content content) {
        if (presenter != null && !content.isFinish()) {
            presenter.requestSetRead(content.getRcId());
        }
        if (!TextUtils.isEmpty(content.getTips())) {
            if (!BoxManager.getInstance().queryBoxOnceStatusByType(OnceTipType.study_message_tip + content.getRcId())) {
                BoxManager.getInstance().putBoxOnce(OnceTipType.study_message_tip + content.getRcId(), true);
                mContext.startActivity(AnalysisDetailActivityAutoBundle.builder(content.getDetailUrl(), content.getFavourResourceId(), content.getFavourTypeId()).tip(content.getTips()).build(mContext));
                return;
            }
        }
        mContext.startActivity(AnalysisDetailActivityAutoBundle.builder(content.getDetailUrl(), content.getFavourResourceId(), content.getFavourTypeId()).build(mContext));

    }

    private void onClickPlay(Study.Content content) {

        if (!NetworkUtils.isWifiConnected()) {

            MaterialDialog dialog = new MaterialDialog(mContext);
            dialog.setTitle("提示");
            dialog.setMessage("当前正在使用非Wifi网络,是否继续观看");
            dialog.setNegativeButton("取消");
            dialog.setPositiveButton("继续", v -> {
                toAnalysisDetail(content);
            });
            dialog.show();
        } else {
            toAnalysisDetail(content);
        }
    }

}
